package com.puti.doctor.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.puti.common.core.domain.R;
import com.puti.doctor.domian.GoodsSeckill;
import com.puti.doctor.service.IGoodsSeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.puti.common.log.annotation.Log;
import com.puti.common.log.enums.BusinessType;
import com.puti.common.security.annotation.PreAuthorize;

import com.puti.common.core.web.controller.BaseController;
import com.puti.common.core.web.domain.AjaxResult;
import com.puti.common.core.utils.poi.ExcelUtil;
import com.puti.common.core.web.page.TableDataInfo;

/**
 * 商品-秒杀 商品-秒杀Controller
 *
 * @author puti
 * @date 2021-04-21
 */
@CrossOrigin
@RestController
@RequestMapping("/seckill")
public class GoodsSeckillController extends BaseController
{
    @Autowired
    private IGoodsSeckillService goodsSeckillService;

    /**
     * 查询商品-秒杀 商品-秒杀列表
     */
    @PreAuthorize(hasPermi = "system:seckill:list")
    @GetMapping("/list")
    public TableDataInfo list(GoodsSeckill goodsSeckill)
    {
        startPage();
        List<GoodsSeckill> list = goodsSeckillService.selectGoodsSeckillList(goodsSeckill);
        return getDataTable(list);
    }

    /**
     * 导出商品-秒杀 商品-秒杀列表
     */
    @PreAuthorize(hasPermi = "system:seckill:export")
    @Log(title = "商品-秒杀 商品-秒杀", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsSeckill goodsSeckill) throws IOException
    {
        List<GoodsSeckill> list = goodsSeckillService.selectGoodsSeckillList(goodsSeckill);
        ExcelUtil<GoodsSeckill> util = new ExcelUtil<GoodsSeckill>(GoodsSeckill.class);
        util.exportExcel(response, list, "商品-秒杀 商品-秒杀数据");
    }

    /**
     * 获取商品-秒杀 商品-秒杀详细信息
     */
    @PreAuthorize(hasPermi = "system:seckill:query")
    @GetMapping(value = "/{seckillId}")
    public AjaxResult getInfo(@PathVariable("seckillId") Long seckillId)
    {
        return AjaxResult.success(goodsSeckillService.selectGoodsSeckillById(seckillId));
    }

    /**
     * 新增商品-秒杀 商品-秒杀
     */
    @PreAuthorize(hasPermi = "system:seckill:add")
    @Log(title = "商品-秒杀 商品-秒杀", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsSeckill goodsSeckill)
    {
        return toAjax(goodsSeckillService.insertGoodsSeckill(goodsSeckill));
    }

    /**
     * 修改商品-秒杀 商品-秒杀
     */
    @PreAuthorize(hasPermi = "system:seckill:edit")
    @Log(title = "商品-秒杀 商品-秒杀", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsSeckill goodsSeckill)
    {
        return toAjax(goodsSeckillService.updateGoodsSeckill(goodsSeckill));
    }

    /**
     * 删除商品-秒杀 商品-秒杀
     */
    @PreAuthorize(hasPermi = "system:seckill:remove")
    @Log(title = "商品-秒杀 商品-秒杀", businessType = BusinessType.DELETE)
	@DeleteMapping("/{seckillIds}")
    public AjaxResult remove(@PathVariable Long[] seckillIds)
    {
        return toAjax(goodsSeckillService.deleteGoodsSeckillByIds(seckillIds));
    }


    /**
     * 商品进度百分比
     */
    @GetMapping("/percentage")
    public R percentage()
    {
        return goodsSeckillService.percentage();
    }


    /**
     * 商品加入购物车
     */
    @PostMapping("puti-shopping/{uniquelyId}")
    public R addCrt(@PathVariable("uniquelyId") String uniquelyId,String goodsCode){
        return goodsSeckillService.addCart(uniquelyId,goodsCode);
    }

}
